#!/bin/bash
################################################################################
# Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The OpenAirInterface Software Alliance licenses this file to You under 
# the Apache License, Version 2.0  (the "License"); you may not use this file
# except in compliance with the License.  
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-------------------------------------------------------------------------------
# For more information about the OpenAirInterface (OAI) Software Alliance:
#      contact@openairinterface.org
################################################################################
# file build_spgw
# brief
# author Lionel Gauthier
# company Eurecom
# email: lionel.gauthier@eurecom.fr
#

set -o pipefail

################################
# include helper functions
################################
THIS_SCRIPT_PATH=$(dirname $(readlink -f $0))
source $THIS_SCRIPT_PATH/../build/tools/build_helper.spgw

LIBGTPNL="LIBGTPNL"
LIBGTPNL_OVS="LIBGTPNL_OVS"
OPENFLOW_MOSAIC="OPENFLOW_MOSAIC"
OPENFLOW="OPENFLOW"
REST="REST"
GTPU_API=$OPENFLOW

function help()
{
  echo_error " "
  echo_error "Usage: build_spgw [OPTION]..."
  echo_error "Build the SPGW executable."
  echo_error " "
  echo_error "Options:"
  echo_error "Mandatory arguments to long options are mandatory for short options too."
  echo_error "  -b, --build-type                          Build type as defined in cmake, allowed values are: Debug Release RelWithDebInfo MinSizeRel"
  echo_error "  -c, --clean                               Clean the build generated files: config, object, executable files (build from scratch)"
  echo_error "  -f, --force                               No interactive script for installation of software packages."
  echo_error "  --gtpu       api                          GTPV1-U implementation, choice in [$LIBGTPNL, $OPENFLOW_MOSAIC, $OPENFLOW], default is $GTPU_API"
  echo_error "  -h, --help                                Print this help."
  echo_error "  -i, --check-installed-software            Check installed software packages necessary to build and run S/P-GW (support $SUPPORTED_DISTRO)."
  echo_error "  -v, --verbose                             Build process verbose."
  echo_error "  -V, --Verbose                             CMake only build process verbose, display compilation warnings and errors."
  echo_error " "
}

function main()
{
  local -i clean=0
  local -i force=0
  local -i verbose=0
  local -i var_check_install_oai_software=0
  local    cmake_args=" "
  export make_args="-j`nproc`"


  until [ -z "$1" ]
    do
    case "$1" in
      -b | --build-type)
        list_include_item "Debug Release RelWithDebInfo MinSizeRel" $2
        [[ $? -ne 0 ]] && echo_error "Build type $2 not recognized" && return $?
        cmake_args="$cmake_args -DCMAKE_BUILD_TYPE=$2"
        shift 2;
        ;;
      -c | --clean)
        clean=1
        echo "Clean the build generated files (build from scratch)"
        shift;
        ;;
      -f | --force)
        force=1
        echo "Force set (no interactive)"
        shift;
        ;;
      --gtpu)
        list_include_item "$LIBGTPNL $OPENFLOW $OPENFLOW_MOSAIC" $2
        [[ $? -ne 0 ]] && echo_error "GTPV1U API type $2 not recognized or not available" && return $?
        GTPU_API=$2
        shift 2;
        ;;
      -h | --help)
        help
        shift;
        return 0
        ;;
      -i | --check-installed-software)
        echo "Check installed software packages necessary to build and run S/P-GW (support $SUPPORTED_DISTRO):"
        set_openair_env
        var_check_install_oai_software=1
        shift;
        ;;
      -v | --verbose)
        echo "Make build process verbose"
        cmake_args="$cmake_args -DCMAKE_VERBOSE_MAKEFILE=ON"
        make_args="VERBOSE=1 $make_args"
        verbose=1
        shift;
        ;;
      -V | --Verbose)
        echo "CMake build process verbose"
        verbose=1
        shift;
        ;;
      *)   
        echo "Unknown option $1"
        help
        return 1
        ;;
    esac
  done

  cmake_args="$cmake_args -DENABLE_$GTPU_API=1"

  export ENABLE_"$GTPU_API"=1
  
  if [ ! -d /usr/local/etc/oai ]; then
    $SUDO mkdir -m 777 -p /usr/local/etc/oai
  fi
  
  if [ $var_check_install_oai_software -gt 0 ];then
    disable_ipv6
    check_install_spgw_software  $force
    if [[ $? -ne 0 ]]; then
        echo_error "Error: SPGW software installation failed"
        return 1
    else
        echo_success "SPGW software installation successful"
        echo_warning "SPGW not compiled, to compile it, re-run build_spgw without -i option"
        return 0
    fi
  fi
  
  set_openair_env 
  
  local dlog=$OPENAIRCN_DIR/build/log
  
  mkdir -m 777 -p $dlog

  ############################################################################
  # Create spgw
  ##############################################################################
  cd $OPENAIRCN_DIR/build
  if [ ! -d ./spgw ]; then
    mkdir -m 777 -p -v spgw
  fi
  ##############################################################################
  # Clean
  ##############################################################################
  cd $OPENAIRCN_DIR/build/spgw
  if [ $clean -ne 0 ]; then
    if [[ $verbose -eq 1 ]]; then
      echo "Cleaning S/P-GW: generated configuration files, obj files, spgw executable"
    fi
    rm -Rf $OPENAIRCN_DIR/build/spgw/build  2>&1
    mkdir -m 777 -p -v build
  fi

  ##############################################################################
  # Compile S/P-GW
  ##############################################################################
  cd $OPENAIRCN_DIR/build/spgw
  if [ ! -d ./build ]; then
    mkdir -m 777 -p -v build
  fi
  
  cd ./build
  $CMAKE $cmake_args .. > /dev/null
  ret=$?;[[ $ret -ne 0 ]] && return $ret

  compilations spgw spgw $OPENAIRCN_DIR/build/spgw/build/spgw $verbose
  ret=$?;[[ $ret -ne 0 ]] && return $ret

  # For daemon should not be group writable
  $SUDO chmod 755 $OPENAIRCN_DIR/build/spgw/build/spgw
  $SUDO cp -upv $OPENAIRCN_DIR/build/spgw/build/spgw /usr/local/bin && $SUDO chmod 755 /usr/local/bin/spgw && echo_success "spgw installed"
  return 0
}


main "$@"
